package com.youdoneed.community.forum.web;

import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.community.common.vo.PageData;
import com.youdoneed.community.forum.vo.ForumlikespointrecordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.forum.entity.Forumlikespointrecord;
import com.youdoneed.community.forum.service.ForumlikespointrecordService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * 论坛点赞模块控制器
 *
 * @author lid
 * @date 2017.6.6
 */
@RestController
@RequestMapping(value = "/likesPointRecord", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "论坛点赞模块控制器", description = "论坛点赞模块相关操作")
public class ForumlikespointrecordController {

    private static final Logger logger = LoggerFactory.getLogger(ForumlikespointrecordController.class);

    @Autowired
    ForumlikespointrecordService forumlikespointrecordService;

    /**
     * 前台保存点赞信息
     *
     * @param forumlikespointrecord
     * @return ResultMessage
     * @author lid
     * @date 2017.6.6
     */
    @ResponseBody
    @RequestMapping(value = "/saveLikesPoint", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "保存点赞信息！likesType：给帖子点赞：0；给评论点赞：1；给新闻点赞：2",
            value = "保存点赞信息！likesType：给帖子点赞：0；给评论点赞：1；给新闻点赞：2")
    public ResultMessage<?> saveLikesPoint(@RequestBody Forumlikespointrecord forumlikespointrecord) {
        Integer result = forumlikespointrecordService.saveForumlikespointrecord(forumlikespointrecord);
        logger.info("result:" + result);
        switch (result) {
            case 1:
                return ResultMessage.newFailure("帖子已删除！");
            case 2:
                return ResultMessage.newFailure("已点赞！");
            case 3:
                return ResultMessage.newSuccess("点赞成功");
            case 4:
                return ResultMessage.newFailure("评论已删除！");
            case 5:
                return ResultMessage.newFailure("点赞失败！");
            default:
                return ResultMessage.newFailure("点赞失败！");
        }
    }

    /**
     * 用户取消点赞
     *
     * @param userId
     * @param sourceId  帖子或者评论的id
     * @param likesType (0:猿圈帖子，1：其他的回复)
     * @return ResultMessage
     * @author lid
     * @date 2017.6.16
     */
    @RequestMapping(value = "/cancelLikesPoint", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "用户取消点赞，其中sourceId为帖子或者评论的id，likesType参数为点赞的类型，0:猿圈帖子，1：其他的回复", value = "用户取消点赞")
    public ResultMessage<?> cancelLikesPoint(String userId, String sourceId, String likesType) {
        logger.info("cancelLikesPoint！");
        int cancel = forumlikespointrecordService.cancelLikesPoint(userId, sourceId, likesType);
        if (cancel != 0) {
            return ResultMessage.newSuccess("取消点赞记录成功！");
        }
        return ResultMessage.newFailure("取消点赞记录失败！");
    }

    /**
     * 获取用户对新闻帖子的点赞记录
     *
     * @param userId
     * @param postsId
     * @return
     */
    @RequestMapping(value = "/hasPraised", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "判断用户是否已经对新闻点赞！0：未点赞；1：已点赞", value = "判断用户是否已经对新闻点赞！0：未点赞；1：已点赞")
    public ResultMessage<Integer> hasPraised(String userId, String postsId) {
        logger.info("cancelLikesPoint！");
        Integer hasPraised = forumlikespointrecordService.hasPraised(userId, postsId);
        logger.info("hasPraised:" + hasPraised);
        return ResultMessage.newSuccess("获取点赞记录成功！").setData(hasPraised);
    }

    @RequestMapping(value = "/getLikesRecordByMsgId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "根据帖子id获取点赞信息", value = "根据帖子id获取点赞信息")
    public ResultMessage<PageData> getLikesRecordByMsgId(@RequestHeader(value = "Authorization") String token,
                                                         String forumMsgId, Integer currentPage, Integer rows) {

        String userId = JWTUtils.getTokenUserId(token);
        if (!StringUtils.hasText(userId)) {
            return ResultMessage.newFailure("获取用户信息失败");
        }

        return ResultMessage.newSuccess("获取点赞列表成功！").
                setData(forumlikespointrecordService.getLikesRecordByMsgId(userId, forumMsgId, currentPage, rows));
    }
}
